#include <bits/stdc++.h>
using namespace std;

// #define ONLINE_JUDGE

#ifndef ONLINE_JUDGE
#define dbg(x...)                             \
	{                                         \
		cerr << "\033[32;1m" << #x << " -> "; \
		err(x);                               \
	}
void err()
{
	cerr << "\033[39;0m" << endl;
}
template <typename T, typename... A>
void err(T a, A... x)
{
	cerr << a << ' ';
	err(x...);
}
#else
#define dbg(...)
#endif

typedef long long LL;

const int N = 1e5 + 5;

int n;
vector<int> a[N];

int b[N];
bool checked[N];

pair<int, int> c[N];

int main(int argc, char const *argv[])
{
	scanf("%d", &n);

	LL ans = 0;
	int cnt = 0;
	for (int i = 0; i < n; ++i)
	{
		int l;
		scanf("%d", &l);
		a[i].resize(l);
		b[i] = 0x7fffffff;
		c[i].first = -b[i];
		c[i].second = 0;
		bool flag = false;
		for (int j = 0; j < l; ++j)
		{
			scanf("%d", &a[i][j]);
			if (a[i][j] > b[i])
			{
				flag = true;
			}
			b[i] = min(b[i], a[i][j]);
			c[i].first = max(c[i].first, a[i][j]);
		}
		if (flag)
		{
			c[i].second = 1;
			ans += n;
			++cnt;
			checked[i] = true;
		}
	}
	sort(c, c + n);

	// for (int i=0; i<n; ++i)
	// 	dbg(i, b[i], c[i], c+n-upper_bound(c, c+n, b[i]));
	// for (int x : cx) dbg(x);

	for (int i=1; i<n; ++i) c[i].second += c[i-1].second;

	for (int i = 0; i < n; ++i)
	{
		dbg(i, checked[i], ans);
		if (!checked[i])
		{
			int pos = upper_bound(c, c+n, make_pair(b[i], 0x7fffffff)) - c;
			ans += n-pos;
			if (pos > 0) ans += c[pos-1].second;
		}
	}
	// ans -= cx.size();
	printf("%lld\n", ans);

	return 0;
}